package com.auction.service.impl;

import com.auction.common.dao.MyBaseServiceImpl;
import com.auction.entity.ProductClassify;
import com.auction.mapper.ProductClassifyMapper;
import com.auction.service.ProductClassifyService;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

@Service
public class ProductClassifyServiceImpl extends MyBaseServiceImpl<ProductClassify, ProductClassifyMapper> implements ProductClassifyService {

    @Override
    public List<Map> querysify() {
        return baseMapper.querysify(0);
    }

    @Override
    public int updateClassifyStatus(int id, int status) {

        ProductClassify productClassify = new ProductClassify();
        productClassify.setId(id);
        productClassify.setIs_featured(status);
        return baseMapper.updateClassify(productClassify);
    }

    @Override
    public int updateClassifyImage(int id, String image) {

        ProductClassify productClassify = new ProductClassify();
        productClassify.setId(id);
        productClassify.setImage(image);
        return baseMapper.updateClassify(productClassify);
    }


    @Override
    public int saveEntity(ProductClassify entity) {

        //判断是否具有父类
        if (entity == null) return 0;
        //没有父类
        if (entity.getParent_id()==0){
            //查找当前最大编码
            String maxCode = baseMapper.findMaxCode("");
            int num = 0;
            if (maxCode!=null)
                num = Integer.parseInt(maxCode.split("/")[1]);
            String code = "/"+ String.format("%02d",(num+1))+"/";
            entity.setCode(code);
        } else { //有父类 需要查找本父类下面的最大编码
            //查找父类的编码
            String pCode = baseMapper.findParentCode(entity.getParent_id());
            String maxCode = baseMapper.findMaxCode(pCode);
            int num = 0;
            if (maxCode!=null)
                num = Integer.parseInt(maxCode.split("/")[2]);
            String code = pCode+ String.format("%03d",(num+1))+"/";
            entity.setCode(code);
        }

        return super.saveEntity(entity);
    }

}
